package com.xcity.pomelo.response;

import java.util.ArrayList;
import java.util.List;

import com.xcity.db.entity.Player;
import com.xcity.db.entity.Ranking;
import com.xcity.game.battle.pve.template.StagePageTemplate;
import com.xcity.game.player.cache.PlayerCacheService;

import naga.x.App;
import naga.x.game.template.TemplateService;
import naga.x.net.impl.pomelo.PomeloMessage;
import naga.x.net.impl.pomelo.protocol.Message;

@PomeloMessage(type = Message.Type.RESPONSE)
public class PVEStageAreaResponse extends Response {
	private int reachPage;
	private int maxArea;
	private int reachArea;
	private int area;
	private int type;
	private String areaMap;
	private String name;
	private List<StagePageInfo> pageList;
	private List<FriendInfo> friendInfo;
	
	public static class StagePageInfo{
		public int page;
//		public String name;
		public int x;
		public int y;
		public StagePageInfo(StagePageTemplate temp){
			this.page = temp.getId() - 1;
//			this.name = temp.getPageName();
			this.x = temp.getAreaX();
			this.y = temp.getAreaY();
		}
	}
	
	public static class FriendInfo{
		public long id;
		public String icon;
		public int page;
		public FriendInfo(long id, String icon, int page){
			this.id = id;
			this.icon = icon;
			this.page = page;
		}
	}
	
	public PVEStageAreaResponse(int serial, int reachPage, int reachArea, int maxArea, int area, int type, List<StagePageTemplate> list, List<Ranking> ranklist) {
		super(serial);
		this.reachPage = reachPage;
		this.maxArea = maxArea;
		this.reachArea = reachArea;
		this.area = area;
		this.type = type;
		this.areaMap = list.get(0).getAreaTemplate().getAreaMap();
		this.name = list.get(0).getAreaTemplate().getName();
		this.pageList = new ArrayList<StagePageInfo>();
		for(int i = 0, size = list.size(); i < size; i++){
			StagePageInfo info = new StagePageInfo(list.get(i));
			this.pageList.add(info);
		}
		friendInfo = new ArrayList<FriendInfo>();
		if(ranklist != null && !ranklist.isEmpty()){
			int off = 0, len = 10;
			PlayerCacheService pcs = App.getApp().getServiceManager().get(PlayerCacheService.class);
			TemplateService ts = App.getApp().getServiceManager().get(TemplateService.class);
			if(ranklist.size() < len){
				len = ranklist.size();
			}
			for(int i = off, to = off + len; i < to; i++){
				Ranking ranking = ranklist.get(i);
				Player p = pcs.find(ranking.getPlayerId());
				if(p != null){
					int ppage = p.getPveStages().getCurrentPage((byte) type);//0~X
					int parea = ts.get(StagePageTemplate.class, ppage + 1).getArea();
					if(area == parea){
						FriendInfo info = new FriendInfo(p.getId(), p.getIcon(), ppage);
						friendInfo.add(info);
					}
				}
			}
		}
	}

	public int getReachPage() {
		return reachPage;
	}

	public void setReachPage(int reachPage) {
		this.reachPage = reachPage;
	}


	public int getReachArea() {
		return reachArea;
	}

	public void setReachArea(int reachArea) {
		this.reachArea = reachArea;
	}

	public int getMaxArea() {
		return maxArea;
	}

	public void setMaxArea(int maxArea) {
		this.maxArea = maxArea;
	}

	public int getArea() {
		return area;
	}

	public void setArea(int area) {
		this.area = area;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public String getAreaMap() {
		return areaMap;
	}

	public void setAreaMap(String areaMap) {
		this.areaMap = areaMap;
	}

	public List<StagePageInfo> getPageList() {
		return pageList;
	}

	public void setPageList(List<StagePageInfo> pageList) {
		this.pageList = pageList;
	}

	public List<FriendInfo> getFriendInfo() {
		return friendInfo;
	}

	public void setFriendInfo(List<FriendInfo> friendInfo) {
		this.friendInfo = friendInfo;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
}
